
var creaGrafico = function(cat, data1, data2, data3, data4, data5){
	$('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Cruscotto statistico Pathways'
        },
        subtitle: {
            text: 'grafico delle risposte'
        },
        xAxis: {
        	categories: cat
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Percentuale risposte (%)'
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y} %</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: [{
            name: 'Risp. A',
            data: data1
        }, {
            name: 'Risp. B',
            data: data2
        }, {
            name: 'Risp. C',
            data: data3
        }, {
            name: 'Risp. D',
            data: data4
        },{
            name: 'Risp. E',
            data: data5
        }]
    });
};

var getData = function(){
	$.ajax({
	   url: '../appcontroller.php?cmd=statistiche',
	   success: function(result) {
		   
		   if(result){
			   var res = eval('('+result+')');
			   
			   var categories = [];
			   var data1 = [];
			   var data2 = [];
			   var data3 = [];
			   var data4 = [];
			   var data5 = [];
			   
			   for(var i=0; i<res.length;i++){
				   categories.push('Slide '+res[i].num_slide);
				   data1.push(parseInt(res[i].risp1));
				   data2.push(parseInt(res[i].risp2));
				   data3.push(parseInt(res[i].risp3));
				   data4.push(parseInt(res[i].risp4));
				   data5.push(parseInt(res[i].risp5));
			   }
			   
			   creaGrafico(categories, data1, data2, data3, data4, data5);
		   }
	   },
	   fail: function(){
		   alert('fail');
	   }
	});
};

$(function () {
	getData();
	setInterval(function(){
		getData();
	}, 120000)
	
});